package com.ibeeking.found.upms.b.rest.controller.v1;

import com.ibeeking.found.upms.b.rest.service.ITenantPermissionService;
import com.ibeeking.found.upms.b.rest.service.ITenantUserService;
import com.ibeeking.found.upms.service.common.param.*;
import com.ibeeking.found.upms.service.common.vo.*;
import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.constants.GlobalConstant;
import com.ibeeking.found.common.constants.HeaderNounConstant;
import com.ibeeking.found.common.db.annotation.IgnoreDS;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.common.entity.LoginUserDTO;
import com.ibeeking.found.upms.api.common.query.AccountLoginQuery;
import com.ibeeking.found.upms.service.common.query.SelectTenantUserQuery;
import com.ibeeking.found.upms.service.common.query.TenantUserPageQuery;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.base.BaseValidationGroup;
import com.ibeeking.nematos.utils.data.TreeFastUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @ClassName UserController控制器
 * @Description 全局用户管理
 * @Author ibeeking
 * @Date 2020-12-02 15:29
 **/
@RestController
@RequestMapping("/b/user/v1")
public class TenantUserController {

    @Resource
    private ITenantUserService userServiceImpl;
    @Resource
    private ITenantPermissionService permissionServiceImpl;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取当前登录用户权限菜单")
    @GetMapping(value = "/currentPermission")
    public List<CurrentPermissionVO> currentPermission(@LoginUser(isFull = true) CurrentUser currentUser, HttpServletRequest request) {
        List<CurrentPermissionVO> result = permissionServiceImpl.queryEffectiveList(currentUser, request.getHeader(HeaderNounConstant.JWT_JTI));
        return TreeFastUtils.listToTree(result, GlobalConstant.PARENT_ID);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取当前登录用户信息")
    @IgnoreDS
    @GetMapping(value = "/currentInfo")
    public CurrentUserVO currentInfo(@LoginUser CurrentUser currentUser) {
        return userServiceImpl.currentInfo(currentUser.getId());
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "当前用户租户列表")
    @IgnoreDS
    @GetMapping(value = "/queryTenantsByUserId")
    public List<TenantListVO> queryTenantsByUserId(@LoginUser CurrentUser currentUser) {
        return userServiceImpl.queryTenants(currentUser.getId());
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "分页查询租户用户列表")
    @PostMapping(value = "/pageList")
    public Page<GmcUserPageVO> pageList(@RequestBody TenantUserPageQuery query) {
        return userServiceImpl.pageList(query);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询用户有效列表")
    @PostMapping(value = "/selectList")
    public List<TenantUserVO> selectList(@RequestBody SelectTenantUserQuery query) {
        return userServiceImpl.selectList(query);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "新增租户用户")
    @PostMapping(value = "/add")
    public Boolean add(@RequestBody @Validated(BaseValidationGroup.Insert.class) TenantUserParam param, @RequestHeader("tenant_id") String tenantId) {
        return userServiceImpl.add(Integer.valueOf(tenantId), param);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改个人信息")
    @IgnoreDS
    @PostMapping(value = "/modifySelf")
    public Boolean modifySelf(@LoginUser CurrentUser currentUser, @RequestBody TenantUserParam tenantUserParam) {
        return userServiceImpl.modifySelf(currentUser.getId(), tenantUserParam);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改租户用户")
    @PostMapping(value = "/modify")
    public Boolean modify(@RequestBody @Validated(BaseValidationGroup.Update.class) TenantUserParam tenantUserParam) {
        return userServiceImpl.modify(tenantUserParam);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询租户用户信息")
    @GetMapping(value = "/view/{id}")
    public TenantUserVO view(@PathVariable("id") Long id) {
        return userServiceImpl.view(id);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "启用/禁用租户用户")
    @GetMapping(value = "/changeStatus/{id}/{status}")
    public Boolean changeStatus(@LoginUser CurrentUser currentUser, @PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return userServiceImpl.changeStatus(id, status, currentUser.getId());
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "注销租户用户(老方法弃用2021-11-19)")
    @IgnoreDS
    @GetMapping(value = "/logout/{tenantId}")
    public Boolean logout(@LoginUser CurrentUser currentUser, @PathVariable("tenantId") String tenantId) {
        return userServiceImpl.logout(tenantId, currentUser.getId());
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "注销租户用户(新方法启用2021-11-19)")
    @IgnoreDS
    @PostMapping(value = "/logoutUser")
    public Boolean logoutUser(@LoginUser CurrentUser currentUser, @RequestBody @Validated LogoutUserParam param) {
        param.setUserId(currentUser.getId());
        return userServiceImpl.logout(param);
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "离职租户用户")
    @GetMapping(value = "/quit/{id}")
    public Boolean quit(@LoginUser CurrentUser currentUser, @PathVariable("id") Long id) {
        if (currentUser.getId().equals(id)) {
            throw new BusinessException("禁止离职自己");
        }
        return userServiceImpl.quit(id);
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "删除租户用户")
    @GetMapping(value = "/del/{id}")
    public Boolean del(@LoginUser CurrentUser currentUser, @PathVariable("id") Long id) {
        if (currentUser.getId().equals(id)) {
            throw new BusinessException("禁止删除自己");
        }
        return userServiceImpl.del(id);
    }

    /**
     * 重置密码
     *
     * @param id
     * @return
     */
    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "重置租户用户密码")
    @GetMapping(value = "/resetPwd/{id}")
    public Boolean resetPwd(@PathVariable("id") Long id) {
        return userServiceImpl.resetPwd(id);
    }

    /**
     * 修改密码
     *
     * @param userModifyPwdParam
     * @return
     */
    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改租户用户密码")
    @PostMapping(value = "/modifyPwd")
    public Boolean modifyPwd(@LoginUser CurrentUser currentUser, @RequestBody @Validated UserModifyPwdParam userModifyPwdParam) {
        return userServiceImpl.modifyPwd(currentUser.getId(), userModifyPwdParam);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "找回租户用户密码")
    @IgnoreDS
    @PostMapping(value = "/findPwd")
    public Boolean findPwd(@RequestBody @Validated UserFindPwdParam userFindPwdParam) {
        return userServiceImpl.findPwd(userFindPwdParam);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询租户用户角色id列表")
    @GetMapping(value = "/queryRoleIdsByUserId/{userId}")
    public List<String> queryRoleIdsByUserId(@PathVariable("userId") @NotNull(message = "userId不能为空") Long userId) {
        return userServiceImpl.queryRoleIdsByUserId(userId);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询是否存在账号")
    @IgnoreDS
    @GetMapping(value = "/queryAccount/{mobile}")
    public Boolean del(@PathVariable("mobile") @NotBlank(message = "手机号必填") String mobile) {
        AccountLoginQuery query = new AccountLoginQuery();
        query.setMobile(mobile);
        LoginUserDTO loginUserDTO = userServiceImpl.queryAccountLogin(query);
        return null != loginUserDTO.getId();
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改手机号")
    @PostMapping(value = "/modifyMobile")
    public Boolean modifyMobile(@LoginUser CurrentUser currentUser, @RequestBody @Validated ModifyMobileParam param) {
        return userServiceImpl.modifyMobile(currentUser.getId(), param);
    }
}
